<?php
/**
 * Created by PhpStorm.
 * User: laowang
 * Date: 2018/3/23
 * Time: 下午1:20
 */

namespace app\home\controller;
use app\home\model\Course;
use app\home\model\CourseSign;
use app\home\model\WechatUser;
/**
 * Class Sign
 * @package app\home\controller
 * 签到
 */
class Sign extends Base
{
    /**
     * 我的签到 首页
     */
    public function index(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $CourseSign = new CourseSign();
        $arr1 =  [];  // 状态值
        $arr2 = [];  // 扣分
        $list = $CourseSign->where(['status' => 0,'openid' => $userId])->field('c_id,id,openid')->select();
        if ($list){
            foreach ($list as $value){
                $end_time = Course::where('id',$value['c_id'])->value('end_time');
                if ($end_time < time()){
                    array_push($arr1,$value['id']);
                    array_push($arr2,$value['openid']);
                }
            }
        }
        if ($arr1){
            $CourseSign->where(['id' => ['in',$arr1]])->update(['status' => 3]);
        }
        if ($arr2){
            WechatUser::where(['openid' => ['in',$arr2]])->setDec('score',3);
        }

        $now = $CourseSign->get_list(['openid' => $userId,'status' => 0]);
        $go = $CourseSign->get_list(['openid' => $userId,'status' => ['neq',0]]);
        $this->assign('go',$go);  // 已完成
        $this->assign('now',$now);  // 未完成
        return $this->fetch();
    }
    /**
     * 详情
     */
    public function detail(){
        $userId = session('userId');
        $this->jssdk();
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $id = input('id');
        $msg = Course::where('id',$id)->find();
        $msg['project_name'] = str_replace('——','、',$msg['project_name']);
        $msg['subject'] = str_replace('——','、',$msg['subject']);
        $msg['module'] = str_replace('——','、',$msg['module']);
        switch (date('N',$msg['start_time'])){
            case 1:
                $msg['week'] = "星期一";
                break;
            case 2:
                $msg['week'] = "星期二";
                break;
            case 3:
                $msg['week'] = "星期三";
                break;
            case 4:
                $msg['week'] = "星期四";
                break;
            case 5:
                $msg['week'] = "星期五";
                break;
            case 6:
                $msg['week'] = "星期六";
                break;
            case 7:
                $msg['week'] = "星期日";
                break;
        }
        $Sign = CourseSign::where(['c_id' => $id,'openid' => $userId])->field('status,signin_time')->find();
        $msg['sign_status'] = $Sign['status'];
        $msg['signin_time'] = $Sign['signin_time']?date('Y-m-d H:i:s',$Sign['signin_time']):'暂无';
        $this->assign('msg',$msg);
        return $this->fetch();
    }
    /**
     * 列表 更多
     */
    public function more(){
        $userId = session('userId');
        $len = input('length');
        $CourseSign = new CourseSign();
        $type = input('type'); // 1 未完成 2 已完成
        if ($type == 1){
            $where = ['openid' => $userId,'status' => 0];
        }else{
            $where = ['openid' => $userId,'status' => ['neq',0]];
        }
        $res = $CourseSign->get_list($where,$len);
        if (!empty($res)){
            return ['code' => 1,'data' => $res];
        }else{
            return ['code' => 0,'data' => []];
        }
    }
    /**
     * 请假操作
     */
    public function play(){
        $userId = session('userId');
        $id = input('id');
        $status = CourseSign::where(['c_id'=> $id,'openid' => $userId])->value('status');
        if ($status == 0){
            CourseSign::where(['c_id'=> $id,'openid' => $userId])->update(['status' => 4,'create_time' => time()]);
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
}